package com.jyw.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.common.constant.SysDictTypeConstant;
import com.jyw.common.core.domain.entity.SysDictData;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.DictNameUtil;
import com.jyw.common.utils.DictUtils;
import com.jyw.system.domain.SysConfig;
import com.jyw.system.domain.SysReceiveDepartConfig;
import com.jyw.system.domain.vo.SysReceiveDepartConfigVo;
import com.jyw.system.domain.vo.SysReceiveDepartDetailVo;
import com.jyw.system.enums.OtherConfigEnum;
import com.jyw.system.mapper.SysReceiveDepartConfigMapper;
import com.jyw.system.service.ISysConfigService;
import com.jyw.system.service.ISysDictDataService;
import com.jyw.system.service.ISysReceiveDepartConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author Created by ZhangCheng on 2023/08/17 (系统工单接单科室配置)
 */
@Service
public class SysReceiveDepartConfigServiceImpl extends ServiceImpl<SysReceiveDepartConfigMapper, SysReceiveDepartConfig> implements ISysReceiveDepartConfigService {

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private ISysDictDataService sysDictDataService;

    @Override
    public SysReceiveDepartDetailVo getDefaultConfig() {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.RECEIVE_DEPART.getKey());
        if (sysConfig != null && StrUtil.isNotEmpty(sysConfig.getConfigValue())) {
            return JSONObject.parseObject(sysConfig.getConfigValue(), SysReceiveDepartDetailVo.class);
        }
        return new SysReceiveDepartDetailVo();
    }

    @Override
    public void updateConfig(SysReceiveDepartDetailVo sysReceiveDepartDetailVo) {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.RECEIVE_DEPART.getKey());
        String configValue = JSONObject.toJSONString(sysReceiveDepartDetailVo);
        if (sysConfig != null) {
            sysConfig.setConfigValue(configValue);
            sysConfig.setUpdateTime(DateUtils.getNowDate());
            sysConfigService.updateConfig(sysConfig);
        } else {
            sysConfigService.initConfig(configValue, OtherConfigEnum.RECEIVE_DEPART);
        }
    }

    @Override
    public List<SysReceiveDepartConfigVo> queryAllList() {
        List<SysReceiveDepartConfigVo> list=this.baseMapper.queryAllList();
        if(CollUtil.isNotEmpty(list)){
            List<SysDictData> dictCache = DictUtils.getDictCache(SysDictTypeConstant.OBS_FAULT_TYPE);
            list.forEach(result->{
                String faultTypeName= DictNameUtil.getNameByValues(result.getFaultType(),dictCache);
                result.setFaultTypeName(faultTypeName);
            });
        }
        return list;
    }

    @Override
    public int insert(SysReceiveDepartConfig sysReceiveDepartConfig) {
        sysReceiveDepartConfig.setCreateTime(DateUtils.getNowDate());
        return this.baseMapper.insert(sysReceiveDepartConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertList(List<SysReceiveDepartConfig> configList) {
        List<SysReceiveDepartConfig> oldList=this.list();
        if (CollectionUtil.isNotEmpty(oldList)) {
            List<Long> ids = oldList.stream().map(SysReceiveDepartConfig::getId).collect(Collectors.toList());
            this.baseMapper.deleteBatchIds(ids);
        }
        if (CollectionUtil.isNotEmpty(configList)) {
            AtomicInteger index = new AtomicInteger(0);
            configList.forEach(config->{
                int newIndex = index.get() + 1;
                index.set(newIndex);
                config.setSort(newIndex);
                config.setCreateTime(DateUtils.getNowDate());
            });
           return this.saveBatch(configList);
        }
        return null;
    }

    @Override
    public int deleteBatchByIds(Long[] ids) {
        return this.baseMapper.deleteBatchByIds(ids);
    }
}
